<template>
	
	
	
	

	<view class="content">
		<uni-card >
			<uni-search-bar @confirm="search" :focus="true"  > </uni-search-bar>
		</uni-card>
		<uni-card  extra="" >
			<uni-list :border="true" >
				<!-- 显示圆形头像 -->
				<uni-list-chat v-for="value in memberList " clickable @click="memberOnClick(value.id)"   :title="value.memberPhone" :avatar="value.avatar" :note="value.memberName" :time="value.createTime" ></uni-list-chat>
			
			</uni-list>
		</uni-card>	
		
				
					
			
		
		
	
	</view>



				
		
							
				
				
			<uni-fab ref="fab" :pattern="pattern"  :horizontal="horizontal" :vertical="vertical"
						:direction="direction"   @fabClick="topRightClick"/>
</template>

<script>
	import http from '@/store/http.js'
	export default {
			onBackPress(options) {
				if (options.from === 'navigateBack') {  
					return false;  
				}  
				this.back();  
				return true;  
			},
			
			components: {},
			data() {
				return {
					searchValue:"",
					memberList:[],
					currentMember:{
						memberType:0,
						memberValue:0,
						memberId:0
					},
					costMember:{
						memberType:0,
						memberValue:0,
						id:0
					},
					dialogTitle:"",
					cardTypeList: [{
							text: '余额',
							value: 1
						}, {
							text: '次数',
							value: 2
						}],
				
					horizontal: 'right',
					vertical: 'bottom',
					direction: 'horizontal',
					pattern: {
						color: '#7A7E83',
						backgroundColor: '#fff',
						selectedColor: '#007AFF',
						buttonColor: '#007AFF',
						iconColor: '#fff'
					},
					
				
				
				
				}
			},
			onLoad() {
				this.getMemberList();
				
			},
			onShow(){
				
			},
			methods: {
				back() {  
					uni.navigateBack({  
						delta:999  
					});  
				}  ,
				memberOnClick(memberId){
					uni.setStorageSync('memberId', memberId);
					uni.navigateTo({
						url:"/pages/member/memberDetail"
					})
				},
				getMemberList(){
					uni.showLoading({
						title:"数据加载中..."
					})
					var self=this;
					var params={
						memberPhone:self.$data.searchValue
					};
					http.post("/api/member/getMemberList",params,(data)=>{
						if(data.status){
							
							
							self.$data.memberList=data.data;
							
							
						}
						uni.hideLoading();
					});
				},
				showMoneyDialog(type,value){
					this.$data.dialogTitle="当前手机号:"+value.memberPhone;
					
					this.$data.currentMember.memberType=type;
					this.$data.currentMember.memberId=value.id;
					if(type==1){
						this.$refs.moneyDialog.open()
					}else{
						this.$refs.numberDialog.open()
						
					}
					
				},
				moneyDialogClose(type){
					switch(type){
						case 1:
						this.$refs.moneyDialog.close()
						break;
						case 2:
						this.$refs.numberDialog.close()
						break;
						case 3:
						this.$refs.cost1Dialog.close()
						break;
						case 4:
						this.$refs.cost2Dialog.close()
						break;
					}
				
					
				},
				moneyDialogConfirm(val){
					var self=this;
					if(val.length==0){
						uni.showToast({
						  title: "请输入充值金额或次数",
						  icon: "none"
						})
						return false;
					}
					if(self.currentMember.memberType==1){
						val=Math.floor(val*100);
					}
					if(this.isNumeric(val)){
						this.currentMember.memberValue=val;
						http.post("/api/member/addMemberValue",this.currentMember,(data)=>{
							var title="";
							if(data.status){
								title="操作成功"
								self.getMemberList();
								if(self.currentMember.memberType==1){
									self.$refs.moneyDialog.close()
								}else{
									self.$refs.numberDialog.close()
								}
								
							}else{
								title=data.msg;
							}
							uni.showToast({
							  title: title,
							  icon: "none"
							})
							
						});
						
						
					}else{
						uni.showToast({
						  title: "只能输入整数",
						  icon: "none"
						})
						return;
					}
				},
				cost(type,value){
					this.$data.dialogTitle="当前手机号:"+value.memberPhone;
					this.$data.costMember.memberType=type;
					if(type==1){
						this.$refs.cost1Dialog.open()
					}else{
						this.$refs.cost2Dialog.open()
						
					}
					
					for(var i=0;i<value.cardList.length;i++){
						var card=value.cardList[i];
						if(card.memberType==type){
							
							this.$data.costMember.id=card.id;
							break;
						}
					}
					
					
					
				},
				costDialogConfirm(val){
					var self=this;
					if(val.length==0){
						uni.showToast({
						  title: "请输入消费金额或次数",
						  icon: "none"
						})
						return false;
					}
					if(self.costMember.memberType==1){
						val=Math.floor(val*100);
					}
					
					if(this.isNumeric(val)){
						this.costMember.memberValue=val;
						http.post("/api/member/useMemberValue",this.costMember,(data)=>{
							var title="";
							if(data.status){
								title="操作成功"
								self.getMemberList();
								if(self.costMember.memberType==1){
									self.$refs.cost1Dialog.close()
								}else{
									self.$refs.cost2Dialog.close()
								}
								
							}else{
								title=data.msg;
							}
							uni.showToast({
							  title: title,
							  icon: "none"
							})
							
						});
						
						
					}else{
						uni.showToast({
						  title: "只能输入整数",
						  icon: "none"
						})
						return;
					}
				},
				search(key){
					this.$data.searchValue=key.value;
					this.getMemberList();
				},
				editMember(item){
					uni.setStorageSync('editMemberInfo', JSON.stringify(item));
					uni.redirectTo({
						url:"/pages/member/editMember"
					})
					
				},
				deleteMember(memberId){
					var self=this;
					uni.showModal({
						title: '删除会员提示',
						content: "确定要删除当前会员吗？删除之后会员信息和余额都会清空。",
						success: function(res) {
							if (res.confirm) {
								http.get("/api/member/deleteMember?memberId="+memberId,(data)=>{
									var title="";
									if(data.status){
										title="操作成功"
										self.getMemberList();
									}else{
										title=data.msg;
									}
									uni.showToast({
									  title: title,
									  icon: "none"
									})
									
								});
							} else if (res.cancel) {
								// console.log('用户点击取消')
							}
						}
					})
				},
				isNumeric(value) {
				  // 使用正则表达式判断是否为数字
				  const regex = /^\d+$/;
				  return regex.test(value);
				},
			
							
						
				
				
				
				topRightClick(){
					uni.navigateTo({
						url:"/pages/member/createMember"
					})
				},
			}
		}
</script>

<style>
	.status_bar {
	        height: var(--status-bar-height);
	        width: 100%;
	    }
.container {
		overflow: hidden;
	}

	.custom-cover {
		flex: 1;
		flex-direction: row;
		position: relative;
	}

	.cover-content {
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		height: 40px;
		background-color: rgba($color: #000000, $alpha: 0.4);
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-left: 15px;
		font-size: 14px;
		color: #fff;
	}

	.card-actions {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		height: 45px;
		border-top: 1px #eee solid;
	}

	.card-actions-item {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.card-actions-item-text {
		font-size: 12px;
		color: #666;
		margin-left: 5px;
	}

	.cover-image {
		flex: 1;
		height: 150px;
	}

	.no-border {
		border-width: 0;
	}
</style>